package cn.shop.crontab;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;

import java.util.List;

/**
 * 本地&线上测试mq配置类
 *
 * @author : 李一涵
 * @date : Created in 2022/5/28 10:11
 * @description :
 */
@Configuration
@ConditionalOnExpression("'${spring.profiles.active:dev}' != 'online'")
public class LocalMQConfig {

    @Bean
    public RocketMQMessageConverter createRocketMQMessageConverter(ObjectMapper objectMapper) {
        RocketMQMessageConverter converter = new RocketMQMessageConverter();
        CompositeMessageConverter mc = (CompositeMessageConverter) converter.getMessageConverter();
        List<MessageConverter> list = mc.getConverters();
        for (MessageConverter mcConverter : list) {
            if (mcConverter instanceof MappingJackson2MessageConverter) {
                MappingJackson2MessageConverter c = (MappingJackson2MessageConverter) mcConverter;
                c.setObjectMapper(objectMapper);
            }
        }
        return converter;
    }
}
